using System;
using System.Reflection;

namespace ClientTools.Client.Controls
{
    public sealed class ControlsManager
    {
        public static ControlInfoCollection GetAllUserControls(string path)
        {
            var returnValue = new ControlInfoCollection();
            foreach (var file in System.IO.Directory.GetFiles(path, "ClientTools.Client.Controls.*.dll"))
            {
                var assembly = Assembly.LoadFile(file);
                foreach (var t in assembly.GetTypes())
                {
                    if (t.IsClass)
                    {
                        var i = t.GetInterface(typeof(IUserControl).Name);
                        if (i != null)
                        {
                            var uc = (IUserControl)Activator.CreateInstance(t);
                            var ci = new ControlInfo
                                         {
                                             Sort = uc.Sort,
                                             TypeName = t.Name,
                                             Type = t,
                                             ButtonText = uc.ButtonText,
                                             ButtonImage = uc.ButtonImage,
                                             ShowDelimiter = uc.ShowDelimiter
                                         };
                            returnValue.Add(ci);
                        }
                    }
                }
            }
            returnValue.Sort(new ControlInfoComparer());
            return returnValue;
        }

        public static ControlInfoCollection GetAllUserControlsNoProfile(string path)
        {
            var returnValue = new ControlInfoCollection();
            foreach (var file in System.IO.Directory.GetFiles(path, "ClientTools.Client.Controls.*.dll"))
            {
                var assembly = Assembly.LoadFile(file);
                foreach (var t in assembly.GetTypes())
                {
                    if (t.IsClass)
                    {
                        var i = t.GetInterface(typeof(IUserControlNoProfile).Name);
                        if (i != null)
                        {
                            var uc = (IUserControlNoProfile)Activator.CreateInstance(t);
                            var ci = new ControlInfo
                                         {
                                             Sort = uc.Sort,
                                             TypeName = t.Name,
                                             Type = t,
                                             ButtonText = uc.ButtonText,
                                             ButtonImage = uc.ButtonImage,
                                             ShowDelimiter = uc.ShowDelimiter
                                         };
                            returnValue.Add(ci);
                        }
                    }
                }
            }
            returnValue.Sort(new ControlInfoComparer());
            return returnValue;
        }
    }
}
